<?php
/**
 * Created by PhpStorm.
 * User: liuser
 * Date: 2018/6/9
 * Time: 上午1:08
 */
/**
 * 爬取币世界资讯
 * User: liuser
 * Date: 2018/4/8
 * Time: 下午3:31
 */
include "vendor/autoload.php";
include "util.php";
require_once   'DBPDO.php';
use Symfony\Component\DomCrawler\Crawler;
DEFINE('URL','http://www.bishijie.com/kuaixun/');
$html = util::curl(URL);
if($html == "" || empty($html)){
    echo "未获取到html" . PHP_EOL;
}
$crawler = new Crawler($html);
$xpath = '.livetop ul';

$crawler->filter($xpath)->each(function ($node,$i){
    $time = $node->attr('id');
    $title = $node->filter('h2')->text();
    $content = $node->filter('div')->text();
    $content = str_replace('《币世界》行情显示，','',$content);
    if(!strpos($content,'查看详情')){
        save_db('币世界',$title,$content,$time);
    }
});


/**
 * 保存到数据库
 */
 function save_db($service_name,$title,$content,$create_at)
{
    $create_at = date('Y-m-d H:i:s',$create_at);
    $dbh = new DBPDO();
    $search = "select id from coin_news where `title` = '{$title}'";
    $search_res  = $dbh->fetch($search);
    if(!$search_res) {
        $sql = "INSERT INTO `coin_news` (`service_name`, `title`, `content`, `create_at`) 
                VALUES ('{$service_name}','{$title}','{$content}','{$create_at}');";
        $res = $dbh->execute($sql);
    }

}
